package com.example.springaidemo.controller.case4;

import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.zhipuai.ZhiPuAiChatModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @author stone
 * @date 2025/7/4 9:48
 */
@RestController
@RequestMapping("/case4")
@Slf4j
public class Case4Controller {

//    public Case4Controller(ZhiPuAiChatModel chatModel,
//                           ChatMemory chatMemory) {
//        this.chatClient = ChatClient
//                .builder(chatModel)
//                .defaultAdvisors(
//                        PromptChatMemoryAdvisor.builder(chatMemory).build()
//                )
//                .build();
//    }

    private ChatClient chatClient;

    /**
     * 对话记忆功能-基于内存
     * <p>
     * MessageWindowChatMemory：默认最大20
     * InMemoryChatMemoryRepository：使用map
     */
//    @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    @GetMapping(value = "/chat")
    public Flux<String> chat(@RequestParam("input") String input) {
        // input=我叫什么
        return this.chatClient.prompt()
                .user(input)
                .stream()
                .content();
    }

    /**
     * 对话记忆功能-区分不同用户
     */
    @GetMapping(value = "/chat2")
    public Flux<String> chat2(@RequestParam("input") String input,
                              @RequestParam("userId") String userId) {
        return this.chatClient.prompt()
                .user(input)
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, userId))
                .stream()
                .content();
    }


//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////


    public Case4Controller(ZhiPuAiChatModel chatModel,
                           ChatMemory chatMemory) {
        this.chatClient = ChatClient
                .builder(chatModel)
                .defaultAdvisors(
                        PromptChatMemoryAdvisor.builder(chatMemory).build()
                )
                .build();
    }

    /**
     * 对话记忆功能-基于redis
     */
    @GetMapping(value = "/chat3")
    public Flux<String> chat3(@RequestParam("input") String input,
                              @RequestParam("userId") String userId) {
        return this.chatClient.prompt()
                .user(input)
                .advisors(a -> a.param(ChatMemory.CONVERSATION_ID, userId))
                .stream()
                .content();
    }

}
